#include "test_tts.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "cm_http.h"

//#define TEST_LOCAL_TTS_SUPPORT

#ifdef TEST_LOCAL_TTS_SUPPORT
#include "cm_local_tts.h"
#endif

static const char tts_url[32] = "http://aip.baidubce.com";                  //仅支持百度"http://aip.baidubce.com"
static const char tts_apiKey[32] = "lqD9Q4Zw4oADZatXxkNGaSkg";              //用户配置
static const char tts_secretKey[64] = "xn5xOFrsypio5tQaRO4iYCdTUkfnszzG";   //用户配置

/*fail cfg*/
// static const char tts_url[32] = "http://aip.baidubce.com";                  //仅支持百度"http://aip.baidubce.com"
// static const char tts_apiKey[32] = "I1Gme5d4qoSLMRXkGMzGGn6Z";              //用户配置
// static const char tts_secretKey[64] = "os30EIFEghri38FojEVVdwKcI1dQAQ1w";   //用户配置
static char *cb_param = "local tts"; 
#define EVENT_TO_STR(event) case event: return #event
static char *event_num_to_str(int event)
{
    switch(event)
    {
        EVENT_TO_STR(CM_TTS_EVENT_SYNTH_DATA);
        EVENT_TO_STR(CM_TTS_EVENT_SYNTH_FAIL);
        EVENT_TO_STR(CM_TTS_EVENT_SYNTH_INTERRUPT);
        EVENT_TO_STR(CM_TTS_EVENT_SYNTH_FINISH);
        EVENT_TO_STR(CM_TTS_EVENT_PLAY_FAIL);
        EVENT_TO_STR(CM_TTS_EVENT_PLAY_INTERRUPT);
        EVENT_TO_STR(CM_TTS_EVENT_PLAY_FINISH);
        default:
            break;
    }
}

static void test_tts_callback(cm_tts_event_e event, void *param)
{
    cm_demo_printf("event:%s, param:%s\n",event_num_to_str(event),param);
}
#ifdef TEST_LOCAL_TTS_SUPPORT
static void test_local_tts_callback(cm_local_tts_event_e event, void *param)
{
    switch (event)
    {
        case CM_LOCAL_TTS_EVENT_SYNTH_DATA:
        {
            cm_local_tts_synth_data_t *synth_data = (cm_local_tts_synth_data_t *)param;
            //cm_demo_printf("event:%s, param_data:%s, param_len:%d, param_user:%s\n",event_num_to_str(event),synth_data->data,synth_data->len,synth_data->user);
            cm_demo_printf("event:%s, param_len:%d, param_user:%s\n",event_num_to_str(event),synth_data->len,synth_data->user);
        }
            break;
        case CM_LOCAL_TTS_EVENT_SYNTH_FAIL:
        case CM_LOCAL_TTS_EVENT_SYNTH_INTERRUPT:
        case CM_LOCAL_TTS_EVENT_SYNTH_FINISH:
        case CM_LOCAL_TTS_EVENT_PLAY_FAIL:
        case CM_LOCAL_TTS_EVENT_PLAY_INTERRUPT:
        case CM_LOCAL_TTS_EVENT_PLAY_FINISH:
        cm_demo_printf("event:%s, param:%s\n",event_num_to_str(event),param);
        default:
            break;
    }
    
}
#endif
void cm_test_tts_operation(unsigned char*cmd[],int len)
{
    unsigned char operation[20] = {0};
    sprintf(operation, "%s", cmd[2]);

    if (0 == strcmp(operation, "INIT"))
    {
        
        
        cm_tts_cfg_t cfg = {0};
        cfg.speed = atoi(cmd[3]);          /*!< 语速，范围根据实际情况确定,范围0-15 */
        cfg.volume = atoi(cmd[4]);         /*!< TTS转化时音量，范围根据实际情况确定,范围0-15 */
        cfg.encode = atoi(cmd[5]);         /*!< 中文文本编码方式,（百度在线TTS）暂仅支持(且必须)设置为2（不生效，实际使用urlencode格式编码） */
        cfg.rdn = atoi(cmd[6]);                       /*!< 数字播放模式,暂仅支持(且必须)设置为0 */
        cfg.pitch = atoi(cmd[7]);          /*!< 音调,范围0-15 */
        cfg.voice = atoi(cmd[8]);          /*!< 音效,范围0-4 */
        cfg.channel = atoi(cmd[9]);                   /*!< 播放通道，暂仅支持(且必须)设置为2（不生效，本平台仅有一个音频输出通道） */
        cfg.url = tts_url;              /*!< TTS语音转换平台网址，仅支持百度在线TTS，配置其他URL不生效 */
        cfg.apiKey = tts_apiKey;                       /*!< client_id （即注册账号的apikey) */
        cfg.secretKey = tts_secretKey;                    /*!< client_secret (secretKey) */
        cm_demo_printf("cm_tts_init ret:%d\n",cm_tts_init(&cfg));
        cm_demo_printf("tts init:speed %d,volume %d,encode %d,rdn %d\n" , cfg.speed, cfg.volume, cfg.encode, cfg.rdn);

    }
    else if(0 == strcmp(operation, "DEINIT"))
    {
       cm_tts_deinit(); 
       cm_demo_printf("cm_tts_deinit no return"); 
    }
    else if (0 == strcmp(operation, "PLAY"))
    {
        uint8_t *uri_encode = cm_httpclient_uri_encode(cmd[3], strlen(cmd[3]));

        cm_demo_printf("%s\n",uri_encode);
        osDelay(2);
        cm_demo_printf("cm_tts_play ret:%d\n",cm_tts_play(uri_encode, strlen(uri_encode), test_tts_callback, "tts test param"));          //播放TTS语音
        osDelay(2);
        cm_free(uri_encode);
        // cm_demo_printf("cm_tts_play ret:%d\n",cm_tts_play("ZHONGWEN%E4%B8%AD%E6%96%87123%E8%B5%B7%E6%9D%A5", strlen("ZHONGWEN%E4%B8%AD%E6%96%87123%E8%B5%B7%E6%9D%A5"), test_tts_callback, "tts test param"));
        
    }else if(0 == strcmp(operation, "STOP"))
    {
       cm_demo_printf("cm_tts_play_stop ret:%d\n", cm_tts_play_stop()); 
    }else if(0 == strcmp(operation, "STATE"))
    {
        cm_demo_printf("cm_tts_get_state ret:%d\n",cm_tts_get_state());
    }
#ifdef TEST_LOCAL_TTS_SUPPORT 
    else if(0 == strcmp(operation, "LOCAL_INIT"))
    {
        cm_local_tts_cfg_t cfg = {
            .speed = atoi(cmd[3]),                      /*!< 语速，范围0~15 */
            .volume = atoi(cmd[4]),                     /*!< TTS转化时音量，范围0~15 */
            .encode = atoi(cmd[5]),       /*!< 中文文本编码方式 */
            .digit = atoi(cmd[6]),         /*!< 数字播放模式 */
            1,//cm_local_tts_tone_e tone;           /*!< 音调，暂不支持 */
            1,//cm_local_tts_effect_e effect;       /*!< 音效，暂不支持 */
        };
        int32_t local_tts_init = cm_local_tts_init(&cfg);
        cm_demo_printf("cm_local_tts_init ret:%d\n",local_tts_init);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,local_tts_init,cm_local_tts_init);
    }else if(0 == strcmp(operation, "LOCAL_DEINIT"))
    {
        cm_demo_printf("cm_local_tts_deinit no ret.\n");
        cm_local_tts_deinit();
    }
    else if(0 == strcmp(operation, "LOCAL_PLAY"))
    {
        const char *text = cmd[3];
        int32_t len = strlen(cmd[3]);
        cm_local_tts_callback cb = test_local_tts_callback;
        int32_t local_tts_play = cm_local_tts_play(text,len,cb,cb_param);
        cm_demo_printf("cm_local_tts_play ret:%d\n",local_tts_play);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,local_tts_play,cm_local_tts_play);
    }
    else if(0 == strcmp(operation, "LOCAL_STOP"))
    {
        int32_t tts_play_stop = cm_local_tts_play_stop();
        cm_demo_printf("cm_local_tts_play_stop ret:%d\n",tts_play_stop);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,tts_play_stop,cm_local_tts_play_stop);
    }
    // else if(0 == strcmp(operation, "LOCAL_STATE"))
    // {
    //     cm_local_tts_state_e local_tts_get_state = cm_local_tts_get_state();
    //     cm_demo_printf("cm_local_tts_get_state ret:%d\n",local_tts_get_state);
    //     OCTEST_FUNC_EXPECT_EQUAL_INT32(2,local_tts_get_state,cm_local_tts_get_state);
    // }
#endif
    else{
        cm_demo_printf("param error!\n");
    }
}

